%%% Copyright 2010 Google Inc.
%%%
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%%     http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.

%%% @copyright 2010 Google Inc. All Rights Reserved.
%%% @author <jacoby@google.com> Jacoby Thwaites [http://sites/madcloud]
%%% @version {@version}
%%% @doc
%%% Tests for the process_cache module.
%%% etc
%%% @headerfile "../include/txm.hrl"
%%% @headerfile "../include/process.hrl"

-module(process_cache_tests).

-include_lib("eunit/include/eunit.hrl").
-include("txm.hrl").
-include("cache_srv.hrl").
-include("process.hrl").

process_cache_test_() ->

  % Tests depend on being sequential to track stats.
  {inorder,

   % This tuple specifies a setup fun(), teardown fun() and list of test fun().
   {setup,

    % Setup starts the cache server backed by a mock prog server.
    fun() ->
         prog_srv:start_link(prog_mock),
         cache_srv:start_link(?PROCESS_CACHE)
    end,

    % Cleanup stops servers.
    fun(_) ->
         cache_srv:stop(?PROCESS_CACHE),
         prog_srv:stop()
    end,

    % Tests.
    [
      % Check initial stats are zero.
      fun() ->
           ?assertMatch(#?CACHE_STATE{name = ?PROCESS_CACHE,
                                      hits = 0,
                                      misses = 0},
                        cache_srv:get_cache_state(?PROCESS_CACHE))
      end,

      % Check ETS table is created correctly. This is read by clients
      % in-process.
      fun() ->
           ?assertEqual('$end_of_table', ets:first(?PROCESS_CACHE))
      end,

      % Cache miss: Check cache record is generated correctly.
      fun() ->
           ?assertMatch(#?PROCESS_CACHE{},
                        cache_srv:get_cache_record(?PROCESS_CACHE,
                                                   {folder, {0, 20, 1}}))
      end,

      % Cache hit: Check folder ref.
      fun() ->
           #?PROCESS_CACHE{folderRef = Ref} =
              cache_srv:get_cache_record(?PROCESS_CACHE,
                                         {folder, {0, 20, 1}}),
           ?assertEqual({folder, {0, 20, 1}}, Ref)
      end,

      % Cache hit: Check digraph size.
      fun() ->
           #?PROCESS_CACHE{graph = Graph} =
              cache_srv:get_cache_record(?PROCESS_CACHE,
                                         {folder, {0, 20, 1}}),
           ?assertEqual(5, digraph:no_vertices(Graph))
      end,

      % Cache hit: Check vertices ETS
      fun() ->
           #?PROCESS_CACHE{vertices = Vertices} =
              cache_srv:get_cache_record(?PROCESS_CACHE,
                                         {folder, {0, 20, 1}}),
           ?assert(lists:member({size, 5}, ets:info(Vertices)))
      end,

      % Check cache stats.
      fun() ->
           ?assertMatch(#?CACHE_STATE{name = ?PROCESS_CACHE,
                                      hits = 3,
                                      misses = 1},
                        cache_srv:get_cache_state(?PROCESS_CACHE))
      end
      ]}}.
